﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace StudentManagement.Models
{
    public class MockStudentRepository : IStudentRepository
    {
        private List<Student> students;

        public MockStudentRepository()
        {
            students = new List<Student>() { 
                new Student()
                {
                    Id = 1, Name = "张三", Major = "计算机科学"                   
                },
                new Student()
                {
                    Id = 2, Name = "李四", Major = "人工智能"
                },
                new Student()
                {
                    Id = 3, Name = "王五", Major = "电子商务"
                }
            };

        }

        public Student Create(Student student)
        {
            student.Id = students.Max(s => s.Id) + 1;            
            students.Add(student);
            return student;
        }

        public Student Delete(int id)
        {
            var student = this.GetStudent(id);
            if (student != null)
            {
                students.Remove(student);
            }
            return student;
        }

        public IEnumerable<Student> GetAllStudents()
        {
            return students;
        }

        public Student GetStudent(int id)
        {
            return students.FirstOrDefault(s => s.Id == id);
        }

        public Student Update(Student student)
        {
            Student s = this.GetStudent(student.Id);
            if (s != null)
            {
                s.Name = student.Name;
                s.Major = student.Major;
            }

            return s;
        }
    }
}
